<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    custom-class="page-dialog sys-user-dialog"
  >
    <el-form
      ref="sysUserForm"
      :rules="rules"
      :model="form"
      label-position="right"
      label-width="80px"
      class="page-form"
      style="width: 100%;"
    >

      <el-form-item label="编号" prop="id">
        <el-input v-model="form.id" placeholder="请输入用户账号" clearable :readonly="!isAdd" />
      </el-form-item>
      <el-form-item label="您的称呼" prop="name">
        <el-input v-model="form.name" placeholder="请输入用户名称" clearable :readonly="isDetail" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号码" clearable :readonly="isDetail" />
      </el-form-item>
      <el-form-item  label="到店人数" prop="peopleNum">
        <el-input v-model="form.peopleNum" type="peopleNum" placeholder="请输入到店人数" clearable :readonly="isDetail" />
      </el-form-item>
      <el-form-item label="到店时间" prop="arriveTime">
        <el-date-picker
          v-model="form.arriveTime"
          type="datetime"
          placeholder="请输入到店时间"
        >
        </el-date-picker>
      </el-form-item>
     <!-- <el-form-item label="备注" prop="notes">
        <el-input v-model="form.notes" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入备注" :readonly="isDetail" />
      </el-form-item>-->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <template v-if="isDetail">
        <el-button type="primary" @click="dialogVisible=false">关闭</el-button>
      </template>
      <template v-else-if="isAdd">
        <el-button @click="restForm()">取消</el-button>
        <el-button type="primary" @click="submitAddForm()">保存</el-button>
      </template>
      <template v-else-if="isUpdate">
        <el-button @click="restForm()">取消</el-button>
        <el-button type="primary" @click="submitUpdateForm()">修改</el-button>
      </template>
    </div>
  </el-dialog>
</template>

<script>


  import sysSubscribeApi from "@/api/system/sys-subscribe-api"
  export default {
    name: 'SysUser',
    components: { },
    props: {
      // detail: 详细页面，add: 添加页面，update：编辑页面
      isDetail: {
        type: Boolean,
        default: false
      },
      isAdd: {
        type: Boolean,
        default: false
      },
      isUpdate: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        dialogVisible: false,
        dialogTitle: null,
        form: {
          id: null,
          name: null,
          peopleNum: null,
          phone: null,
          arriveTime: null,
          notes: null
        },

        updateId: null,
        treeSelectData: null,
        rules: {
          username: [
            { required: true, message: '请输入用户账号', trigger: 'blur' },
            { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            { min: 11, max: 11, message: '长度11个字符', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '请选择状态', trigger: 'change' }
          ],
          remark: [
            { max: 100, message: '长度100个字符', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {},
    created() {

    },
    mounted() {

    },
    methods: {
      handle(id) {
        if (this.isAdd) {
          this.rules = null;
          this.dialogTitle = '添加预约表';
          // 新智不需要获取信息
          // this.getUserDetail(id);
        } else if (this.isDelete) {
          this.dialogTitle = '删除预约';
        } else if (this.isUpdate) {
          this.dialogTitle = '修改预约';
          this.updateId = id;
          this.getUserDetail(id);
        }
        this.dialogVisible = true
      },
      submitAddForm() {
        console.log("form...")
        this.$refs.sysUserForm.validate((valid) => {
          if (valid) {
            console.log('submit!')
            this.addUser()
            // this.$emit("getList")
          } else {
            console.log('error submit!!')
            return false
          }
        });
      },
      submitUpdateForm() {
        this.$refs.sysUserForm.validate((valid) => {
          if (valid) {
            console.log('submit!')
            this.updateUser()
            // this.$emit("getList")
          } else {
            console.log('error submit!!')
            return false
          }
        });
      },
      getUserDetail(id) {
        sysSubscribeApi.detail(id).then(response => {
          if (response.code === 200) {
            this.form = response.data;
            // if (this.isDetail) {
            //   this.form.id = response.data.id
            // }
          }
        });
      },
      updateUser() {
        const updateParam = {
          id: this.form.id,
          phone: this.form.phone,
          name: this.form.name,
          peopleNum: this.form.peopleNum,
          arriveTime: this.form.arriveTime,
          notes: this.form.notes
        }
        sysSubscribeApi.update(updateParam).then(response => {
          if (response.code === 200) {
            this.restForm();
            this.$message({
              message: '修改预约表成功',
              type: 'success'
            })
            this.$emit('change')
          }
        })
      },
      deleteUser() {
        const deleteParam = {
          id: this.form.id,
          phone: this.form.phone,
          name: this.form.name,
          peopleNum: this.form.peopleNum,
          arriveTime: this.form.arriveTime,
          notes: this.form.notes
        }
        sysSubscribeApi.delete(deleteParam).then(response => {
          if (response.code === 200) {
            this.restForm();
            this.$message({
              message: '刪除预约表成功',
              type: 'success'
            })
            this.$emit('change')
          }
        })
      },
      addUser() {
        const addParam = {
          id: this.form.id,
          phone: this.form.phone,
          name: this.form.name,
          peopleNum: this.form.peopleNum,
          arriveTime: this.form.arriveTime,
          notes: this.form.notes
        }
        sysSubscribeApi.add(addParam).then(response => {
          if (response.code === 200) {
            this.restForm();
            this.$message({
              message: '新增预约表成功',
              type: 'success'
            })
            this.$emit('change')
          }
        })
      },
      restForm() {
        this.$refs.sysUserForm.resetFields()
        this.dialogVisible = false
      }
    }
  }
</script>

<style lang="scss">
  .sys-user-dialog {
    margin-top: 40px !important;
  }
</style>

<style lang="scss" scoped>

</style>
